package com.dlc.felear.xiaoensale.helper;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.DoubleInputDialog;
import com.dlc.felear.xiaoensale.customView.JbSelectDialog;
import com.dlc.felear.xiaoensale.customView.PowerSelectDialog;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.entity.ActionStepItem;

public class SelectActionHelper implements SingleInputDialog.SingleDialogCallback,
        DoubleInputDialog.DoubleDialogCallback , JbSelectDialog.JbSelectDialogCallback,
        PowerSelectDialog.PowerSelectDialogCallback {
    private static final String TAG="SelectActionHelper";
    private static final int WHAT_CANCEL_DIALOG=343;
    private AppCompatActivity context;
    private SingleInputDialog  singleInputDialog;
    private String name;
    private DoubleInputDialog doubleInputDialog;
    private JbSelectDialog jbSelectDialog;
    private PowerSelectDialog powerSelectDialog;
    public SelectActionHelper(AppCompatActivity context) {
        this.context = context;
        singleInputDialog=new SingleInputDialog(context,this);
        doubleInputDialog=new DoubleInputDialog(context,this);
        jbSelectDialog=new JbSelectDialog(context,this);
        powerSelectDialog=new PowerSelectDialog(context,this);
    }

    public void parserAction(int type, ActionStepItem item) {
        name= item.getName();
        switch (type) {
            case 1:
                //锅体动作
                gtdzPaser(item);
                break;
            case 2:
                //固体添加
                jGtPaser(item);
                break;
            case 3:
                //液体添加
                ytdcPaser(item);
                break;
            case 4:
                //酱料添加
                ytdcPaser(item);
                break;
        }
    }


    /**
     * 锅体动作
     *
     * @param item
     */
    private void gtdzPaser(ActionStepItem item) {
        switch (item.getName()){
            case Constant.ACTION_NAME_TZJR:
                //停止加热
                sendMsg(item.getName(),0,0);
                break;
            case Constant.ACTION_NAME_KQJR:
                //开始加热
                powerSelectDialog.showDialog(item);
//                doubleDialogShow("输入档位(1-8)", "输入温度(1-150)℃",item.getId(),"档位:","温度:");
                break;
            case Constant.ACTION_NAME_TZJB:
                //停止搅拌
                sendMsg(item.getName(),0,0);
                break;
            case Constant.ACTION_NAME_KSJB:
                //开始搅拌
                jbSelectDialog.showDialog(item);
//                doubleDialogShow("输入档位(1-9)", "1反转 2正转", item.getId(), "转速:", "方向:");
                break;
            case Constant.ACTION_NAME_GSGZ:
                //盖上盖子
                sendMsg(item.getName(),0,0);
                break;
            case Constant.ACTION_NAME_DKGZ:
                //打开盖子
                sendMsg(item.getName(),0xff,0);
                break;
            case Constant.ACTION_NAME_DC:
                //倒菜
                sendMsg(item.getName(),0x30,0);
                break;
            case Constant.ACTION_NAME_XG:
                //洗锅
                sendMsg(item.getName(),0x30,0);
                break;
            case Constant.ACTION_NAME_JSC:
                //加食材
                singleDialogShow("输入格子序号(1-4)",item.getId(),"序号:");
                break;
            case Constant.ACTION_NAME_FW:
                //复位
                singleDialogShow("0:格子复位 1:锅体复位",item.getId(),"类型:");
                break;
            case Constant.ACTION_NAME_CCJD:
                //炒菜
                doubleDialogShow("输入时长", "锅体斜度，数值(1、2、3)",item.getId(),"时长:","角度:");
                break;
            case Constant.ACTION_NAME_JC:
                //炒菜
                singleDialogShow("输入加菜时间",item.getId(),"时长:");
                break;
            case Constant.ACTION_NAME_CCSC:
                //炒菜
                singleDialogShow("输入炒菜时长",item.getId(),"时长:");
//                singleDialogShow("输入（1-100）",item.getId(),"系数:");
                break;
            case Constant.ACTION_NAME_JGG:
                //炒菜
                singleDialogShow("提示:1 加上盖子 2拿掉盖子",item.getId(),"锅盖状态:");
                break;
        }
    }

    private void singleDialogShow(String hint,int id,String tips1){
        singleInputDialog.setHintMsg(hint);
        singleInputDialog.setTips(tips1);
        singleInputDialog.showDialog(id);
    }

    /**
     * 液体动作
     *
     * @param item
     */
    private void ytdcPaser(ActionStepItem item) {
        Log.e(TAG,"液体调料："+JsonHelper.toJson(item));
        singleDialogShow("输入毫升值",item.getSerialNo(),"毫升:");
    }
    /**
     * 加固体动作
     *
     * @param item
     */
    private void jGtPaser(ActionStepItem item) {
        Log.e(TAG,"固体调料："+JsonHelper.toJson(item));
        singleDialogShow("输入多少克",item.getSerialNo(),"重量:");
    }
    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void SingleDialogInputResult(String value,int id) {
        try {
            switch (name){
                case Constant.ACTION_NAME_JSC:
                    sendMsg(name,Integer.parseInt(value),15);
                    break;
                case Constant.ACTION_NAME_Y:
                case Constant.ACTION_NAME_JJ:
                case Constant.ACTION_NAME_T:
                case Constant.ACTION_NAME_LAOCHOU:
                case Constant.ACTION_NAME_HHJY:
                case Constant.ACTION_NAME_HONGYOU:
                case Constant.ACTION_NAME_YOU:
                case Constant.ACTION_NAME_GQZ:
                case Constant.ACTION_NAME_YTJJ:
                case Constant.ACTION_NAME_SHUI:
                case Constant.ACTION_NAME_SHUI1:
                case Constant.ACTION_NAME_KSJB:
                case Constant.ACTION_NAME_CCJD:
                case Constant.ACTION_NAME_JC:
                case Constant.ACTION_NAME_JLA:
                    sendMsg(name,0,getStringValue(value));
                    break;
                case Constant.ACTION_NAME_CCSC:
                    if(getStringValue(value)<0||getStringValue(value)>500){
                        showToast("请输入1-500的正整数");
                    }else {
                        sendMsg(name,0,getStringValue(value));
                    }
                    break;
                default:
                    sendMsg(name,Integer.parseInt(value),0);
            }
        }catch (Exception e){
            showToast("数据解析异常");
        }
    }

    private int getStringValue(String value){
        int tem=0;
        try {
            tem=Integer.parseInt(value);
        }catch (Exception e){

        }
        return tem;
    }

    private void sendMsg(String operaterName,int value,int value1){
        Log.e(TAG,"name="+name+" value1="+value+ " value2="+value1);
        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra(Constant.OPERATOR_NAME, getName(operaterName,value));
        intent.putExtra(Constant.OPERATOR_VALUE_1, value);
        intent.putExtra(Constant.OPERATOR_VALUE_2, value1);
        //设置返回数据
        context.setResult(1024, intent);//RESULT_OK为自定义常量
        //关闭Activity
        context.finish();
    }
    private String getName(String name,int value){
        return name;
    }

    private void doubleDialogShow(String hint1, String hint2, int id,String tips1,String tips2) {
        Log.e(TAG,"hint1="+hint1+" hint2="+hint2);
        doubleInputDialog.setHintMsg(hint1, hint2);
        doubleInputDialog.setTips(tips1,tips2);
        doubleInputDialog.showDialog(id);
    }

    @Override
    public void doubleDialogInputResult(int value1, int value2, int id) {
        sendMsg(name,value1,value2);
    }

    @Override
    public void onJbItemClickResult(ActionStepItem actionStepItem, int result, int direct) {
        sendMsg(name,result,direct);
    }

    @Override
    public void onItemClickResult(ActionStepItem actionStepItem, int result) {
        sendMsg(name,result,Constant.HOT_TEMP_MAX);
    }
}
